﻿using System;
using System.ComponentModel;
using System.Drawing;
using System.Runtime.InteropServices;
using System.Windows.Forms;

namespace QuickRunCore.Helper
{
    [Designer(typeof(MyControl))]
    //[DesignTimeVisible(true)]
    public partial class SkinForm : UserControl
    {
        /// <summary>
        /// 所在窗体
        /// </summary>
        Form PForm = null;

        public event EventHandler CloseButtonClick;

        #region 自定义属性

        [Category("外观")]
        [Description("标题栏背景颜色")]
        [DefaultValue("")]
        [Browsable(true)]
        [DesignerSerializationVisibility(DesignerSerializationVisibility.Visible)]
        public Color TitleBackColor
        {
            get
            {
                return pnlTitle.BackColor;
            }
            set
            {
                pnlTitle.BackColor = value;
            }
        }

        [Category("外观")]
        [Description("标题栏文字")]
        [DefaultValue("")]
        [Browsable(true)]
        [DesignerSerializationVisibility(DesignerSerializationVisibility.Visible)]
        public string TitleText
        {
            get
            {
                return lblTitle.Text;
            }
            set
            {
                lblTitle.Text = value;
                if (PForm != null)
                {
                    PForm.Text = value;
                }
            }
        }

        [Category("外观")]
        [Description("是否允许改变大小")]
        [DefaultValue("")]
        [Browsable(true)]
        [DesignerSerializationVisibility(DesignerSerializationVisibility.Visible)]
        public bool ChangeSize
        {
            get
            {
                return btnRightBottom.Visible;
            }
            set
            {
                btnRightBottom.Visible = value;
            }
        }
        #endregion

        /// <summary>
        /// 获取存放控件的Panel
        /// </summary> 
        //[Browsable(false), DesignerSerializationVisibility(DesignerSerializationVisibility.Content)]
        //public Panel MainPanel
        //{ 
        //    get 
        //    { 
        //        return pnlMain; 
        //    } 
        //}

        public SkinForm()
        {
            InitializeComponent();

        }

        private void SkinForm_Load(object sender, EventArgs e)
        {
            PForm = this.ParentForm;
            //PForm.FormBorderStyle = FormBorderStyle.None;
            //this.Dock = DockStyle.Fill;

            this.picClose.Image = Properties.Resources.BtnClose1;
            this.picClose.MouseEnter += (s, e) => { this.picClose.Image = Properties.Resources.BtnClose2; };
            this.picClose.MouseLeave += (s, e) => { this.picClose.Image = Properties.Resources.BtnClose1; };

            this.btnRightBottom.Image = Properties.Resources.RightBottom;

            #region 右下角拖动窗体大小

            this.btnRightBottom.MouseDown += (s, e) =>
            {
                this.bDragging = true;
                this.mPoint = new Point(e.X, e.Y);
            };
            this.btnRightBottom.MouseMove += (s, e) =>
            {
                Point p = new Point(e.X, e.Y);
                Rectangle r = PForm.ClientRectangle;
                if (bDragging)
                {

                    int w = PForm.Size.Width;
                    int h = PForm.Size.Height;
                    w += (e.X - mPoint.X);
                    h += (e.Y - mPoint.Y);
                    PForm.Size = new Size(w, h);
                    PForm.Invalidate();

                }
            };
            this.btnRightBottom.MouseUp += (s, e) => { bDragging = false; };

            #endregion

            #region FormBorderStyle为None,拖放窗体

            pnlTitle.MouseDown += (s, a) =>
            {
                ReleaseCapture();
                SendMessage(PForm.Handle, WM_SYSCOMMAND, SC_MOVE + HTCAPTION, 0);
            };

            #endregion

            btnRightBottom.Left = this.Width - btnRightBottom.Width;
            btnRightBottom.Top = this.Height - btnRightBottom.Height;
        }

        #region FormBorderStyle为None,拖放窗体

        [DllImport("user32.dll")]
        public static extern bool ReleaseCapture();
        [DllImport("user32.dll")]
        public static extern bool SendMessage(IntPtr hwnd, int wMsg, int wParam, int lParam);
        public const int WM_SYSCOMMAND = 0x0112;
        public const int SC_MOVE = 0xF010;
        public const int HTCAPTION = 0x0002;

        //拖动右下角图标用变量
        bool bDragging;
        Point mPoint;

        #endregion

        private void picClose_Click(object sender, EventArgs e)
        {
            if (CloseButtonClick != null)
            {
                CloseButtonClick(this, e);
            }
        }

        //private void SkinForm_ClientSizeChanged(object sender, EventArgs e)
        //{
        //    pnlMain.Top = pnlTitle.Height;
        //    pnlMain.Left = 0;
        //    pnlMain.Height = this.Height - pnlTitle.Height;
        //    pnlMain.Width = this.Width;
        //}
    }


    public class MyControl : System.Windows.Forms.Design.ParentControlDesigner
    {
        private SkinForm _control;

        public override void Initialize(IComponent component)
        {
            base.Initialize(component);
            _control = (SkinForm)component;
            //bool succ = this.EnableDesignMode(_control.MainPanel, "pnlMain");
            //if (!succ)
            //    throw new Exception("加载控件失败");
        }
    }

}
